/* 行为树节点 */

var ENUM_BTNTRICK_STATUS = {
    NIL:0,
    RUNNING:1,
    FAIL:2
}

var BTNNode = function(id){
    this._id = id;
    this._status   = ENUM_BTNTRICK_STATUS["NIL"]
    this._children = []

    this.addChild = function(child){
        this._children.push(child);
    }

    this.removeChild = function(child){
        var index = this._children.indexOf(child);
        if (index > -1) {
            this.splice(index, 1);
        }
    }

    this.evaluate = function(){
        return false;
    }

    this.tick = function(){
        return ENUM_BTNTRICK_STATUS["NIL"];
    }

    this.clear = function(){
    }

    this.id = function(){
        return this._id;
    }
}

/**
 * seq节点，
 */
var BTNSeqNode = function(id){
    // 实例继承
    BTNNode.call(this, id);

    // 当前节点标记
    this._nidx = 0;

    this.evaluate = function(){
        var isret = false;
        var child;
        for (var childidx in this._children){
            child = this._children[childidx];
            
            if (!child.evaluate()){
                // cc.log(child, "---->>child");
                return false;
            }
        }
        return true;
    }

    this.tick = function(){
        var cnt = this._children.length - 1;
        if (this._nidx > cnt){
            this._nidx = 0;
        }
        
        var status = ENUM_BTNTRICK_STATUS["NIL"]
        var nchild = undefined;
        for (var i=this._nidx; i<cnt; i++){
            nchild = this._children[i];
            status = nchild.tick();
            if (status == ENUM_BTNTRICK_STATUS["NIL"]){
                nchild.clear();
                ++this._nidx;
                continue;
            } 
            else if (status == ENUM_BTNTRICK_STATUS["END"]){
                nchild.clear();
                ++this._nidx;
                break;
            }
            else{
                break;
            }
        }
        return status;
    }

    this.clear = function(){
        this._nidx = 0;
        for (var child in this._children){
            child.clear();
        }
    }
}
BTNSeqNode.prototype = new BTNNode();

/**
 * sele节点
 */
var BTNSeleNode = function(id){
    //实例继承
    BTNNode.call(this, id);

    //当前节点标记
    this._nidx = 0;
    this._actnode = undefined;

    this.evaluate = function(){
        var bret = false;
        var lastnode = this._actnode;
        var child;
        for (var childidx in this._children){
            child = this._children[childidx];
            if (child.evaluate()){
                this._actnode = child;
                bret = true;
                break;
            }
        }
        if (lastnode != undefined && lastnode != this._actnode){
            lastnode.clear();
        }
        return bret;
    }

    this.tick = function(){
        if (this._actnode){
            this._actnode.tick();
        }
        return ENUM_BTNTRICK_STATUS["END"]
    }

    this.clear = function(){
        if (this._actnode){
            this._actnode.clear();
        }
    }
}
BTNSeleNode.prototype = new BTNNode();

/**
 * leat -condition节点
 * @param {bool} bret执行动作之后的固定返回值
 */
var BTNConditionNode = function(id, bret/*  额外参数，动作之后的返回 */){
    // 实例继承
    BTNNode.call(this, id);

    this._bret = bret;
    
    this.evaluate = function(){
        this.doEvalute()
        return this._bret;
    }

    this.tick = function(){
        return ENUM_BTNTRICK_STATUS["NIL"]
    }

    this.doEvalute = function(){
        //todo
    }
}
BTNConditionNode.prototype = new BTNNode();

/**
 * leaf -action节点 
 */
var BTNActionNode = function(id){
    // 实例继承
    BTNNode.call(this, id);

    this.evaluate = function(){
        return true;
    }

    this.tick = function(){
        return ENUM_BTNTRICK_STATUS["RUNNING"]
    }
}
BTNActionNode.prototype = new BTNNode();

/**
 * leaf -execute节点
 */
var BTNExecuteNode = function(id){
    // 实例继承
    BTNNode.call(this);
    
    this.tick = function(){
        return ENUM_BTNTRICK_STATUS["NIL"]
    }

    this.evaluate = function(){
        return true;
    }
}
BTNExecuteNode.prototype = new BTNNode();